package com.example.administrator.daydayappmarket;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.administrator.daydayappmarket.search.fragment.SearchActivityfirst3Fragment;
import com.example.administrator.daydayappmarket.search.fragment.SearchFirstFragment;
import com.example.administrator.daydayappmarket.search.fragment.SearchSecondFragment;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class SearchActivity extends AppCompatActivity {

    @InjectView(R.id.activity_search_back)
    RelativeLayout back;
    @InjectView(R.id.activity_search_edit)
    EditText edit;
    @InjectView(R.id.activity_search_clear)
    ImageView clear;
    @InjectView(R.id.activity_search_view)
    View view;
    @InjectView(R.id.activity_search_search)
    ImageView search;
    @InjectView(R.id.activity_search_fragment)
    FrameLayout fragment;

    private FragmentManager manager;
    private SearchFirstFragment firstFragment;
    private SearchSecondFragment secondFragment;
    private FragmentTransaction fragmentTransaction;
    private SearchActivityfirst3Fragment searchActivityfirst3Fragment;
    private MyReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_search);
        ButterKnife.inject(this);
        firstFragment = new SearchFirstFragment();
        secondFragment = new SearchSecondFragment();
        searchActivityfirst3Fragment = new SearchActivityfirst3Fragment();
        manager = getSupportFragmentManager();
        fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.replace(R.id.activity_search_fragment,firstFragment);
        fragmentTransaction.commit();

        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter("com.appMarket.searchActivity.adapterFirst");
        registerReceiver(receiver,filter);

        //edit文本改变监听器
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!TextUtils.isEmpty(edit.getText())){
                    //显示清空按钮
                    clear.setVisibility(View.VISIBLE);
                    //发送广播  文本改变
                    //TODO  文本本改变监听 有问题？？？
//                    Intent intent = new Intent("com.appMarket.searchActivity.adapterFirst");
//                    intent.putExtra("name",edit.getText().toString());
//                    sendBroadcast(intent);
//
//                    Log.e("tag", "onTextChanged: "+11 );
                }else {
                    clear.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @OnClick({R.id.activity_search_back, R.id.activity_search_clear, R.id.activity_search_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_search_back:
                //点击返回

//                if(fragmentTransaction.isAddToBackStackAllowed()){
//
//                }
                finish();
                break;
            case R.id.activity_search_clear:
                //清空EditText中的内容
                    edit.setText("");
                clear.setVisibility(View.GONE);

                break;
            case R.id.activity_search_search:
                //点击EditText跳入搜索fragment
                if(!TextUtils.isEmpty(edit.getText())){
                    //如果Edit中的内容不为空
                    //点击搜索发送广播
                    Intent intent2 = new Intent("com.appMarket.searchActivity.adapterFirst");
                    intent2.putExtra("name",edit.getText().toString());
                    sendBroadcast(intent2);

                }else {
                    //如果Edit中的内容为空  弹出吐司
                    Toast.makeText(this, "请输入关键字", Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }

    class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {

            //Fragment绑定数据
            searchActivityfirst3Fragment = new SearchActivityfirst3Fragment();
            edit.setText(intent.getStringExtra("name"));
            Bundle bundle = new Bundle();
            bundle.putString("name",intent.getStringExtra("name"));
            searchActivityfirst3Fragment.setArguments(bundle);

            Log.e("tag", "onReceive: "+1111 );
            //动态显示fragment
            fragmentTransaction = manager.beginTransaction();
            fragmentTransaction.replace(R.id.activity_search_fragment,searchActivityfirst3Fragment);
            fragmentTransaction.commit();
        }
    }
}
